//
//  XDLMImmediateInvestmentViewController.m
//  xindailianmeng
//
//  Created by 于于 on 17/2/28.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDLMImmediateInvestmentViewController.h"
#import "XDLMExpectView.h"
#import "XDLMImmediateInvestmentModel.h"

@interface XDLMImmediateInvestmentViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)XDLMImmediateInvestmentModel *model;

@end

@implementation XDLMImmediateInvestmentViewController
{
    UITableView *_table;
    UILabel *_moneyLab; // 金额
    UILabel *_coupon; // 优惠券
    UILabel *_canMoney;//可用金额
    UILabel *_expectMoney;//预计金额
    
    UILabel *_DueToPay;
    NSDictionary *dataDic;
    UITextField *_enterField;
    UIView *_balaceBackView;//余额不足背景view
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kColor_243_243_243;
    self.navigationItem.title = @"立即投资";
    [self loadData];
    [self createRightItem];
    
    // Do any additional setup after loading the view.
    
}
- (void)loadData
{
//    if (self.recordID == nil) {
//        self.recordID = @"0";
//    }
    NSDictionary *param = @{@"method":@"toBuyPost",@"postId":@"31"};
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject)
     {
         NSLog(@"%@",responseObject);
//         NSDictionary *dic = responseObject[@"result"];
         if ([responseObject[@"code"] isEqualToString:@"100000"]) {
             dataDic = [responseObject objectForKey:@"result"];
             self.model = [[XDLMImmediateInvestmentModel alloc] initWithDictionary:dataDic error:nil];
             NSLog(@"%@",self.model);
             [self creatView];
         }
        
     } failure:^(NSURLSessionDataTask *task, NSError *error)
     {
         
     }];
}
//请求预期收益接口
- (void)loadInvestData
{
    
    NSDictionary *param = @{@"method":@"getExpectedEarning",@"postId":@"31",@"investMoney":@"1000",@"userVoucherId":@""};
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject)
     {
         NSLog(@"%@",responseObject);
         //         NSDictionary *dic = responseObject[@"result"];
         if ([responseObject[@"code"] isEqualToString:@"100000"]) {
             
         }
         
     } failure:^(NSURLSessionDataTask *task, NSError *error)
     {
         
     }];
}

-(void)creatView
{
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, (kScreenH - 64)*kscale) style:UITableViewStylePlain];
    [_table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_table setBackgroundColor:kColor_243_243_243];
    [_table setScrollEnabled:NO];
    _table.delegate = self;
    _table.dataSource = self;
    _table.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_table];
    
    UIButton *investnowBtn = [[UIButton alloc] initWithFrame:CGRectMake((kScreenW-244)/2*kscale, 289*kscale, 244*kscale, 45*kscale)];
    [investnowBtn setBackgroundImage:[UIImage imageNamed:@"investnow"] forState:UIControlStateNormal];
    [investnowBtn addTarget:self action:@selector(investnowBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:investnowBtn];
}
//展示余额不页
- (void)creatBalanceView
{
    _balaceBackView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenH, kScreenW, 200*kscale)];
    _balaceBackView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_balaceBackView];
    [UIView animateWithDuration:0.25 animations:^{
        _balaceBackView.frame = CGRectMake(0, kScreenH - 200*kscale, kScreenW, 200*kscale);
    }];
}
//创建navigationItem
- (void)createRightItem
{
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"充值" style:UIBarButtonItemStylePlain target:self action:@selector(RightBtnClick)];
    rightItem.tintColor = [UIColor grayColor];
    
    self.navigationItem.rightBarButtonItem = rightItem;
}
#pragma mark ---点击方法
//点击立即投资
- (void)investnowBtnClick
{
    [self creatBalanceView];
}
-(void)RightBtnClick
{
   
}
#pragma mark ---tableDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0 || indexPath.row == 2 || indexPath.row == 4)
    {
        return 10;
    }
    else if (indexPath.row == 1)
    {
        return 50;
    }
    else if (indexPath.row == 3)
    {
        return 106;
    }
    else
    {
        return 65;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 5) {
        [self loadInvestData];
        XDLMExpectView *ecpect = [[XDLMExpectView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) data:@[@"130.00",@"200.00",@"3000.00"]];
        [[UIApplication sharedApplication].keyWindow addSubview:ecpect];
    }
}
#pragma mark ---预计收益弹框

#pragma mark ---UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0 || indexPath.row == 2 || indexPath.row == 4) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"line"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"line"];
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW*kscale, 10)];
            [cell addSubview:line];
            
            UILabel *line1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenW*kscale, 0.5)];
            UILabel *line2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 9.5, kScreenW*kscale, 0.5)];
            [line1 setBackgroundColor:kColor_219_219_219];
            [line2 setBackgroundColor:kColor_219_219_219];
            [line addSubview:line1];
            [line addSubview:line2];
            [cell setBackgroundColor:[UIColor clearColor]];
        }
        return cell;
    }
    else if(indexPath.row == 1)
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];
             [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            UILabel *lab = [self createLabelWithFrame:CGRectMake(15*kscale, 0, 150*kscale, 50) text:@"投资金额" textFont: [UIFont systemFontOfSize:15] textColor:kColorRGB(102, 102, 102)];
            [lab setTextAlignment:NSTextAlignmentLeft];
            [cell addSubview:lab];
//            _moneyLab = [self createLabelWithFrame:CGRectMake((kScreenW -165)*kscale, 18*kscale, 150*kscale, 15) text:@"100.00元" textFont: [UIFont systemFontOfSize:15] textColor:kColorRGB(51, 51, 51)];
//            [_moneyLab setTextAlignment:NSTextAlignmentRight];
//            [cell addSubview:_moneyLab];
            _enterField = [[UITextField alloc] initWithFrame:CGRectMake(180*kscale, 18*kscale, kScreenW - 195*kscale, 18*kscale)];
            

            
            
        }
        return cell;
    }
    else if (indexPath.row == 3)
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell3"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell3"];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
             UILabel *lab = [self createLabelWithFrame:CGRectMake(15*kscale, 0, 150*kscale, 40) text:@"优惠券" textFont: [UIFont systemFontOfSize:15] textColor:kColorRGB(102, 102, 102)];
            
            _coupon = [self createLabelWithFrame:CGRectMake((kScreenW -183.5)*kscale, 0, 150*kscale, 40) text:@"三张优惠券" textFont: [UIFont systemFontOfSize:15] textColor:kColorRGB(187, 147, 115)];
            [_coupon setTextAlignment:NSTextAlignmentRight];
            [lab setTextAlignment:NSTextAlignmentLeft];
            UILabel *lab1 = [self createLabelWithFrame:CGRectMake(15*kscale, 52*kscale, 150*kscale, 15) text:@"待支付" textFont: [UIFont systemFontOfSize:15] textColor:kColorRGB(102, 102, 102)];
            [lab1 setTextAlignment:NSTextAlignmentLeft];
            
            _coupon = [self createLabelWithFrame:CGRectMake((kScreenW -165)*kscale, 52, 150*kscale, 15) text:@"100.00元" textFont: [UIFont systemFontOfSize:15] textColor:kColorRGB(177, 33, 80)];
            [_coupon setTextAlignment:NSTextAlignmentRight];
            [cell addSubview:_coupon];
            
            UILabel *line1 = [[UILabel alloc] initWithFrame:CGRectMake(15*kscale, 40, kScreenW*kscale, 0.5)];
            [line1 setBackgroundColor:kColor_219_219_219];
            [cell addSubview:lab];
            [cell addSubview:lab1];
            [cell addSubview:line1];
            [cell addSubview:_coupon];
            
            _canMoney = [self createLabelWithFrame:CGRectMake((kScreenW -165)*kscale, 78*kscale, 150*kscale, 15) text:[NSString stringWithFormat:@"可用金额:%.2f",[self.model.available floatValue]] textFont: [UIFont systemFontOfSize:15] textColor:kColorRGB(51, 51, 51)];
            [_canMoney setTextAlignment:NSTextAlignmentRight];
            [cell addSubview:_canMoney];
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenW -23.5)*kscale, 13*kscale, 8.5*kscale, 15*kscale)];
            [image setImage:[UIImage imageNamed:@"right"]];
            [cell addSubview:image];
        }
        return cell;
    }
    else
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell5"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell5"];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            UILabel *lab = [self createLabelWithFrame:CGRectMake(15*kscale, 0, 150*kscale, 65) text:@"预计收益" textFont: [UIFont systemFontOfSize:15] textColor:kColorRGB(102, 102, 102)];
            [lab setTextAlignment:NSTextAlignmentLeft];
            [cell addSubview:lab];
            
            UILabel *lab1 = [self createLabelWithFrame:CGRectMake((kScreenW -183.5)*kscale, 41*kscale, 150*kscale, 10) text:@"每月还息,到期还本" textFont: [UIFont systemFontOfSize:10] textColor:kColorRGB(102, 102, 102)];
            [lab1 setTextAlignment:NSTextAlignmentRight];
            [cell addSubview:lab1];
            
            _expectMoney = [self createLabelWithFrame:CGRectMake((kScreenW -183.5)*kscale, 18*kscale, 150*kscale, 15) text:@"400.00元" textFont: [UIFont systemFontOfSize:15] textColor:kColorRGB(51, 51, 51)];
            [_expectMoney setTextAlignment:NSTextAlignmentRight];
            [cell addSubview:_expectMoney];
            UILabel *line2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 64.5, kScreenW*kscale, 0.5)];
            [line2 setBackgroundColor:kColor_219_219_219];
            [cell addSubview:line2];
            
            [cell addSubview:_expectMoney];
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenW -23.5)*kscale, 25*kscale, 8.5*kscale, 15*kscale)];
            [image setImage:[UIImage imageNamed:@"right"]];
            [cell addSubview:image];
        }
        return cell;
    }
}

#pragma mark --- 生成label
- (UILabel *)createLabelWithFrame:(CGRect)frame text:(NSString *)text textFont:(UIFont *)font textColor:(UIColor *)color {
    
    UILabel * label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.textColor = color;
    label.font = font;
    label.adjustsFontSizeToFitWidth = YES;
    label.textAlignment = NSTextAlignmentCenter;
    
    return label;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
